package cn.iocoder.yudao.module.wine.service.suggest;

import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.suggest.vo.AppSuggestRespVO;
import cn.iocoder.yudao.module.wine.controller.app.suggest.vo.AppSuggestSaveReqVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplytype.SupplyTypeDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.suggest.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.suggest.SuggestDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.suggest.SuggestMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 用户建议 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SuggestServiceImpl implements SuggestService {

    @Resource
    private SuggestMapper suggestMapper;

    @Override
    public Long createSuggest(SuggestSaveReqVO createReqVO) {
        // 插入
        SuggestDO suggest = BeanUtils.toBean(createReqVO, SuggestDO.class);
        suggestMapper.insert(suggest);
        // 返回
        return suggest.getId();
    }

    @Override
    public void updateSuggest(SuggestSaveReqVO updateReqVO) {
        // 校验存在
        validateSuggestExists(updateReqVO.getId());
        // 更新
        SuggestDO updateObj = BeanUtils.toBean(updateReqVO, SuggestDO.class);
        suggestMapper.updateById(updateObj);
    }

    @Override
    public void deleteSuggest(Long id) {
        // 校验存在
        validateSuggestExists(id);
        // 删除
        suggestMapper.deleteById(id);
    }

    private void validateSuggestExists(Long id) {
        if (suggestMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public SuggestRespVO getSuggest(Long id) {
        return suggestMapper.selectJoinOne(SuggestRespVO.class, new MPJLambdaWrapperX<SuggestDO>()
                .eq(SuggestDO::getId, id)
                .leftJoin(MemberUserDO.class, MemberUserDO::getId,SuggestDO::getUserId)
                .leftJoin(SupplyTypeDO.class, SupplyTypeDO::getId,SuggestDO::getSupplyTypeId)
                .selectAll(SuggestDO.class)
                .selectAs(MemberUserDO::getNickname,SuggestRespVO::getNickname)
                .selectAs(SupplyTypeDO::getTypeName,SuggestRespVO::getSupplyTypeName));
    }

    @Override
    public PageResult<SuggestRespVO> getSuggestPage(SuggestPageReqVO pageReqVO) {
        return suggestMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppSuggestRespVO> getSuggestList() {
        return suggestMapper.getMySuggestList();
    }

    @Override
    public Long publishSuggest(AppSuggestSaveReqVO createReqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        createReqVO.setUserId(loginUserId);
        SuggestDO suggest = BeanUtils.toBean(createReqVO, SuggestDO.class);
        suggestMapper.insert(suggest);
        // 返回
        return suggest.getId();
    }

}
